﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms.DanhMuc
{
    public partial class FR_PhuCap : DevExpress.XtraEditors.XtraForm
    {
        string ma_hoc_van = "";
        string new_id = "";
        bool isUpdate = false;
        IDIC_ALLOWANCEData _IDIC_ALLOWANCEData;
        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_PhuCap(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_PhuCap(bool _isUpdate, string mahocvan)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            ma_hoc_van = mahocvan;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IDIC_ALLOWANCEData = (IDIC_ALLOWANCEData)Activator.GetObject(typeof(IDIC_ALLOWANCEData), util.Chuoiphucap);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            
            tangid();

            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id;
            }
        }

        private void tangid()
        {
            new_id = _icommon.Generate_ID("DIC_ALLOWANCE", "AllowanceCode", "PC");
        }

        private void loadthongtin()
        {
            DIC_ALLOWANCE hv = Laythongtin(ma_hoc_van);
            txtma.Text = hv.AllowanceCode;
            txtten.Text = hv.AllowanceName;
            txtghichu.Text = hv.Description;
            txttien.Value = hv.MaximumMoney;
        }

        private DIC_ALLOWANCE Laythongtin(string educode)
        {
            DataSet ds = _IDIC_ALLOWANCEData.DIC_ALLOWANCE_Get(educode);

            DIC_ALLOWANCE hv = new DIC_ALLOWANCE();

            if (ds.Tables.Count>0)
            {
                string ma = ds.Tables[0].Rows[0][0].ToString();
                string ten = ds.Tables[0].Rows[0][1].ToString();
                string tien = ds.Tables[0].Rows[0][2].ToString();
                string ghichu = ds.Tables[0].Rows[0][3].ToString();

                hv.Description = ghichu;
                hv.AllowanceCode = ma;
                hv.AllowanceName = ten;
                hv.MaximumMoney = decimal.Parse(tien);
            }

            return hv;
        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            decimal tien= txttien.Value;
            int kq = _IDIC_ALLOWANCEData.DIC_ALLOWANCE_Insert(ma, ten, tien, ghichu);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            decimal tien = txttien.Value;
            int kq = _IDIC_ALLOWANCEData.DIC_ALLOWANCE_Update(ma, ten, tien, ghichu);
            return kq;
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtghichu.Text = "";
            txtma.Text = new_id;
            txtten.Text = "";
            txttien.Text = "0";
            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;
            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}